package com.lyn.solitude.plugin.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.lyn.solitude.plugin.support.PluginSupport

/**
 *
 * @author longyn 2024/05/16
 * @version 1.0.0
 */
abstract class BaseFragment<V : ViewBinding> : Fragment() {

    lateinit var binding: V

    abstract fun getViewBinding(): V

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = getViewBinding()
        initView(binding)
        return binding.root
    }


    abstract fun initView(binding: V)


    open fun getPluginSupport(): PluginSupport? {
        val requireActivity = requireActivity()
        val clazz = requireActivity::class.java
        val pluginLifecycleApiField = clazz.getDeclaredField("pluginLifecycleApi")
        pluginLifecycleApiField.isAccessible = true
        val any = pluginLifecycleApiField.get(requireActivity)
        if (any is PluginSupport) {
            return any
        }
        return null
    }
}